package com.whfc.ms.api.cfg.controller;

import com.whfc.common.param.ConfigCenterParam;
import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.cfg.service.MsConfigCenterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-07-30 11:38
 */
@RequiresPermissions("inner/cfg")
@RestController
@RequestMapping("/ms/api/cfg")
public class MsConfigCenterController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsConfigCenterService msConfigCenterService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("module") String module) {
        logger.info("获取配置中心配置列表, loginer:{}, deptId:{}, module:{}", user.getUsername(), deptId, module);
        List list = msConfigCenterService.getConfigList(deptId, module);
        return ResultUtil.success(new ListData<>(list));
    }

    @RequiresPermissions("inner/cfg")
    @PostMapping("/configure")
    public Result configure(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestBody @Validated ConfigCenterParam param) {
        logger.info("设置配置中心配置, loginer:{}, param:{}", user.getUsername(), param);
        msConfigCenterService.setConfig(param);
        return ResultUtil.success();
    }


}
